<template>
  <!-- 分配权限的弹层 -->
  <el-dialog
    title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
    :visible="showAssignDialog"
    @close="closeAssignDialog"
  >
    <!-- 权限点数据展示 -->
    <el-tree
      ref="tree"
      :data="permList"
      :props="{ label: 'name' }"
      :default-expand-all="true"
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <template #footer>
      <el-button @click="closeAssignDialog">取消</el-button>
      <el-button type="primary" @click="assignPerm">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
// 获取分配给角色的权限点数据
import { getPermissionListApi } from "@/api/permisson";
// 获取角色之前分配的权限点->回显
import {getRoleDetailApi,assignPermApi} from '@/api/setting'
// 导入树形转化的方法
import { listToTreeData } from "@/utils";
export default {
  props: {
    // 父组件传递控制弹层显隐
    showAssignDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      // 分配给角色的权限点数据列表
      permList: [],
      // 存储角色ID
      roleId:''
    };
  },
  created() {
    this.getPermission();
  },
  methods: {  
    // 给角色分配权限
    async assignPerm() {
       await assignPermApi({
        id:this.roleId,
        permIds: this.$refs.tree.getCheckedKeys()
       })
       this.$message.success('给员工分配权限成功！')
       this.closeAssignDialog()
    },
    // 获取角色之前分配的权限点->回显
    async getRoleDetail(id) {
       const {permIds} = await getRoleDetailApi(id)
       console.log('角色分配之前的权限点',permIds)
       this.roleId = id
       this.$refs.tree.setCheckedKeys(permIds)
    },
    // 获取权限点列表数据
    async getPermission() {
      const permList = await getPermissionListApi();
      // console.log(list);
      this.permList = listToTreeData(permList);
    },
    closeAssignDialog() {
      this.$emit("update:showAssignDialog", false);
    },
  },
};
</script>

<style lang="scss" scoped>
</style>